//
//  QCVisitorView.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/1/17.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit
import SnapKit

class QCVisitorView: UIView {
    //定义一个闭包
    var closure: (()->())?
    
  override init(frame: CGRect) {
        super.init(frame: frame)
         setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //按钮点击事件
    @objc private func buttonClick() {
        //执行闭包
        closure?()
    }
    
    func setupVisiorViewInfo(imgName: String?, title: String?) {
        
        if let img = imgName, let tit = title  {
            iconImageView.image = UIImage(named: img)
            feedImageView.isHidden = true
            messageLabel.text = tit
            
        } else {
        
          setupFeedImageViewAnim()
            
        }
        
        
    }
    func setupFeedImageViewAnim() {
       //设置圆圈动画
       let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置toValue
        anim.toValue = M_PI * 2
        //设置时长
        anim.duration = 20
        //重复次数
        anim.repeatCount = MAXFLOAT
        //切换控制器 或者程序退出后台 默认动画不会移除
        anim.isRemovedOnCompletion = false
        //添加动画
        feedImageView.layer.add(anim, forKey: nil)
    }
    
   private func setupUI() {
    //背景颜色
    backgroundColor = RGB(r: 235, g: 235, b: 235)
    //添加子控件
    addSubview(feedImageView)
    addSubview(maskImageView)
    addSubview(iconImageView)
    addSubview(messageLabel)
    addSubview(loginButton)
    addSubview(registerButton)
    //设置约束
//    feedImageView.snp_makeConstraints { (make) in
//        make.center.equalTo(self)
//    }
    
    feedImageView.snp.makeConstraints { (make) in
        make.center.equalToSuperview()
    }
    
    
//    maskImageView.snp_makeConstraints { (make) in
//        make.center.equalTo(self)
//    }
    maskImageView.snp.makeConstraints { (make) in
        make.center.equalToSuperview()
    }
//    iconImageView.snp_makeConstraints { (make) in
//        make.center.equalTo(self)
//    }
    iconImageView.snp.makeConstraints { (make) in
        make.center.equalToSuperview()
    }
//    messageLabel.snp_makeConstraints { (make) in
//        make.top.equalTo(maskImageView.snp_bottom).offset(16)
//        make.width.equalTo(230)
//        make.centerX.equalTo(self)
//    }
    messageLabel.snp.makeConstraints { (make) in
        make.top.equalTo(maskImageView.snp.bottom).offset(16)
        make.width.equalTo(230)
        make.centerX.equalToSuperview()
    }

    loginButton.snp.makeConstraints { (make) in
        make.left.equalTo(messageLabel)
        make.top.equalTo(messageLabel.snp.bottom).offset(16)
        make.size.equalTo(CGSize(width: 100, height: 35))
    }
    
//    registerButton.snp_makeConstraints { (make) in
//        make.size.equalTo(loginButton)
//        make.top.equalTo(loginButton)
//        make.right.equalTo(messageLabel)
//    }
      registerButton.snp.makeConstraints { (make) in
        make.size.equalTo(loginButton)
        make.top.equalTo(loginButton)
        make.right.equalTo(messageLabel)
    }
    
    
    }
    //懒加载控件
    //圆圈图片
    private lazy var feedImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮挡
    private lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    private lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    lazy var messageLabel: UILabel = {
        
        let lab = UILabel()
        
        lab.font = UIFont.systemFont(ofSize: 14)
        
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.numberOfLines = 0
        lab.textAlignment = .center
        
        return lab
        
    }()
    
    private lazy var loginButton:UIButton = {
       
        let btn = UIButton()
        btn.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        btn.setBackgroundImage(UIImage(named:"common_button_white"), for: UIControlState.normal)
        btn.setTitle("登录", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(KCOlOR, for: UIControlState.highlighted)
        // 设置字体大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return btn
    }()
    
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        btn.setBackgroundImage(UIImage(named:"common_button_white"), for: UIControlState.normal)
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(KCOlOR, for: UIControlState.highlighted)
        // 设置字体大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return btn
        
    }()
    
}
